#include "Keyboard.h"

#include <Keypad.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String input_password = ""; //每次输入的字串
String default_password = "123456"; //默认密码（正确才可以）

String password[] = {"a123456", "b123456", "c123456", "d123456", "e123456", "f123456", "g123456", "h123456"};
String ch_[] = {"taobao", "zhifu", "jd", "xigua", "bili", "bank", "tel-131", "game"};
int ch_no = 0;
bool mode_ch  = false;//自动退出
int  mode = 0; //0默认显示输入密码 1显示选择密码输出
long unsigned lasttime = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A3, A2, A1, A0}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void main_show(String c_) {
  display.clearDisplay();

  display.setTextSize(2);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.println("input:");         // Use full 256 char 'Code Page 437' font
  display.println(c_);
  display.display();
}


void main_password_1 () {
  if (input_password == default_password) {
    display.clearDisplay();
    display.setTextSize(2);      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(0, 0);     // Start at top-left corner
    display.println("password\nis right\nwaiting");         // Use full 256 char 'Code Page 437' font
    display.display();
    delay(1000);
    mode = 1;
    mode_ch = true; //自动退出
    lasttime = millis();
    ch_password_2(ch_no);
  }
  else {
    display.clearDisplay();
    display.setTextSize(2);      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(0, 0);     // Start at top-left corner
    display.println("password\nis ERROR!");         // Use full 256 char 'Code Page 437' font
    display.display();
    delay(1000);
    main_show("******");
  }

}


void ch_password_2(int no) {
  display.clearDisplay();
  display.setTextSize(2);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.println("chiose:");         // Use full 256 char 'Code Page 437' font
  display.println(ch_[no]);
  display.display();
  //delay(1000);
}


void setup() {
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(200); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();
  main_show("******");
  Serial.begin(9600);
  Keyboard.begin();

}


void loop() {
  char key = keypad.getKey();

  if (mode == 0) {
    if (key) {
      lasttime = millis();
      Serial.println(key);
      //Keyboard.write(key);

      if (key == '#')
      {
        main_password_1 ();
        input_password = "";
      }
      else {
        input_password += key;
        main_show(input_password);
      }
    }
  }

  else if (mode == 1) {
    if (key) {
      lasttime = millis();
      if (key == '*') { //选择密码
        if (ch_no >= 8) {
          ch_no == 0;
        }
        ch_no ++;
        ch_password_2(ch_no);
      }
      if (key == '0') { //退出当前模式
        mode = 0;
        main_show("******");
      }
      if (key == '#') {//输出密码
        Keyboard.print( password[ch_no]);
      }
    }
  }
  if (mode_ch == true) {
    if (millis() - lasttime >= 10000) {
      mode = 0;
      mode_ch = false;
      main_show("******");
    }
  }
}
32u4-oled-4x4keypad-password.ino